/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.common.utils;

import com.google.common.base.Preconditions;
import com.jnzc.ocr.common.consts.ErrorConsts;
import com.jnzc.ocr.common.error.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017/8/27
 */
public class ValidateUtils {

    private static final String REGEXP_MOBILE = "1\\d{10}";

    private ValidateUtils(){}

    public static void notEmpty(String data, String dataName){
        notEmpty(data, true, null, null, dataName);
    }

    public static void notEmpty(String data, Integer maxLen, String dataName){
        notEmpty(data, true, null, maxLen, dataName);
    }

    public static void notEmpty(String data, Integer minLen, Integer maxLen, String dataName){
        notEmpty(data, true, minLen, maxLen, dataName);
    }

    public static void notEmpty(String data, Boolean isRequired, Integer minLen, Integer maxLen, String dataName){
        Preconditions.checkNotNull(isRequired);
        Preconditions.checkNotNull(dataName);
        if(StringUtils.isEmpty(data)){
            if(isRequired){
                throw new BusinessException(ErrorConsts.VALIDATE, dataName + "不能为空");
            }
            return;
        }
        if(null != minLen && minLen > data.length()){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "长度必须大于等于" + minLen);
        }
        if(null != maxLen && maxLen < data.length()){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "长度必须小于等于" + maxLen);
        }
    }

    public static <E> void notEmpty(Collection<E> data, String dataName){
        if(CollectionUtils.isEmpty(data)){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "不能为空");
        }
    }

    public static void notNull(Object data, String dataName){
        if(null == data){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "不能为空");
        }
    }

    public static void isMobile(String data){
        isMobile(data, true);
    }

    public static void isMobile(String data, Boolean isRequired){
        Preconditions.checkNotNull(isRequired);
        if(StringUtils.isEmpty(data)){
            if(isRequired){
                throw new BusinessException(ErrorConsts.VALIDATE, "手机号不能为空");
            }
            return;
        }
        if(!RegexpUtils.match(REGEXP_MOBILE, data)){
            throw new BusinessException(ErrorConsts.VALIDATE, "手机号格式不正确");
        }
    }

    public static void isPassword(String data, String dataName){
        ValidateUtils.notEmpty(data, 6, 18, dataName);
    }

    public static void isId(Long data){
        isId(data, "ID");
    }

    public static void isId(Long data, String dataName){
        notNull(data, dataName);
        if(data <= 0){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "必须大于0");
        }
    }

    public static <T> void in(T data, List<T> list, String dataName){
        Preconditions.checkNotNull(list);
        if(!list.contains(data)){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "不正确");
        }
    }

    public static void inRange(Integer data, Integer minLen, String dataName){
        inRange(data, true, minLen, null, dataName);
    }

    public static void inRange(Integer data, Boolean isRequired, Integer minLen, Integer maxLen, String dataName){
        Preconditions.checkNotNull(isRequired);
        Preconditions.checkNotNull(dataName);
        if(null == data){
            if(isRequired){
                throw new BusinessException(ErrorConsts.VALIDATE, dataName + "不能为空");
            }
            return;
        }
        if(null != minLen && minLen > data){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "必须大于等于" + minLen);
        }
        if(null != maxLen && maxLen < data){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "必须小于等于" + maxLen);
        }
    }

    public static void inRange(BigDecimal data, BigDecimal minLen, String dataName){
        inRange(data, true, minLen, null, dataName);
    }

    public static void inRange(BigDecimal data, Boolean isRequired, BigDecimal minLen, BigDecimal maxLen, String dataName){
        Preconditions.checkNotNull(isRequired);
        Preconditions.checkNotNull(dataName);
        if(null == data){
            if(isRequired){
                throw new BusinessException(ErrorConsts.VALIDATE, dataName + "不能为空");
            }
            return;
        }
        if(null != minLen && minLen.compareTo(data) > 0){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "必须大于等于" + minLen);
        }
        if(null != maxLen && maxLen.compareTo(data) < 0){
            throw new BusinessException(ErrorConsts.VALIDATE, dataName + "必须小于等于" + maxLen);
        }
    }

    public static void isTrue(Boolean data, String messgae){
        Preconditions.checkNotNull(data);
        Preconditions.checkNotNull(messgae);
        if(!data){
            throw new BusinessException(ErrorConsts.VALIDATE, messgae);
        }
    }
}
